/* This dofile is to build a dataset of students at baseline */

clear all
capture log close
set more off
set varabbrev off

log using "$logfile/build_grade910_$date.log", replace

clear

**************** DATA PROCESSING ***********************************************

cap drop program _all
program clean_grade910
* Personal information
cap ren whatisyourname name
cap ren whatisyourgender sex
lab define sex 1 "Male" 2 "Female"
lab val sex sex 
cap ren inwhichdzongkhagisyourschoolloca district
cap ren whatisthenameofyoursecondaryscho schoolname
cap ren dateofbirthdaywhatisyourdateofbi b_day
cap ren dateofbirthmonthwhatisyourdateof b_month
cap ren dateofbirthyearwhatisyourdateofb b_year
cap ren whatisyourstudentidcode studentid
cap ren whatisyouremailaddress email
cap ren whatisyourmobilephonenumber phone
cap ren whichdzongkhagareyouoriginallyfr district_origin

* Perception about education and employment
cap ren scienceandtechnologyaremakingour b2a
cap lab var b2a "perception: science makes lives healthier, easier, comfortable"
cap ren allthingsconsideredscienceandtec b2d 
cap lab var b2d "perception: science makes the world better off"
cap ren scienceeducationismoreimportantf b4a
cap lab var b4a "perception: science more important for boy"
cap ren onaveragemenhavehigherabilityins b4b 
cap lab var b4b "perception: men have higher ability in STEMM"
cap ren ifwomenhavestemmsciencetechnolog b4c
cap lab var b4c "perception: STEMM women suffer in personalsocial life"
cap ren stemmrelatedjobsareformenthanwom b4d
cap lab var b4d "perception: STEMM jobs are for men than women"
cap ren whenjobsarescarcemenshouldhavemo b5b
cap lab var b5b "perception: men should have job if scarce"
cap ren whenamotherworksforpaythechildre b5c
cap lab var b5c "perception: mother works for pay, children suffer"
cap ren sciencehowimportantisitforyoutob b6a
cap lab var b6a "importance to be knowledgable about science"
cap ren artsandhumanitieshowimportantisi b6b
cap lab var b6b "importance to be knowledgable about arts"

* Academic performance
cap ren mathwhatisyouracademicperformanc c1a
cap lab var c1a "performance: math"
cap ren dzongkhawhatisyouracademicperfor c1b
cap lab var c1b "performance: dzongkha"
cap ren englishwhatisyouracademicperform c1c
cap lab var c1c "performance: english"
cap ren physicswhatisyouracademicperform c1d 
cap lab var c1d "performance: physics"
cap ren chemistrywhatisyouracademicperfo c1e
cap lab var c1e "performance: chemistry"
cap ren biologywhatisyouracademicperform c1f
cap lab var c1f "performance: biology"
cap ren historyandcivicswhatisyouracadem c1g
cap lab var c1g "performance: history"
cap ren geographywhatisyouracademicperfo c1h
cap lab var c1h "performance: geography"
cap ren overallwhatisyouracademicperform c1i
cap lab var c1i "performance: overall"

* Preference about STEM-related education
cap ren mathhowmuchdoyoulikeeachofthefol c2a
cap lab var c2a "liking: math"
cap ren dzongkhahowmuchdoyoulikeeachofth c2b
cap lab var c2b "liking: dzongkha"
cap ren englishhowmuchdoyoulikeeachofthe c2c
cap lab var c2c "liking: english"
cap ren physicshowmuchdoyoulikeeachofthe c2d
cap lab var c2d "liking: physics"
cap ren chemistryhowmuchdoyoulikeeachoft c2e
cap lab var c2e "liking: chemistry"
cap ren biologyhowmuchdoyoulikeeachofthe c2f
cap lab var c2f "liking: biology"
cap ren historyandcivicshowmuchdoyoulike c2g
cap lab var c2g "liking: history"
cap ren geographyhowmuchdoyoulikeeachoft c2h
cap lab var c2h "liking: geography"
cap ren overallhowmuchdoyoulikeeachofthe c2i
cap lab var c2i "liking: overall"

* Mentoring
cap ren parentsdoyoutalktoaboutyourcaree c3c1
cap lab var c3c1 "mentoring: parents"
cap ren brotherssistersdoyoutalktoabouty c3c2
cap lab var c3c2 "mentoring: siblings"
cap ren otherrelativesdoyoutalktoaboutyo c3c3
cap lab var c3c3 "mentoring: relatives"
cap ren friendsdoyoutalktoaboutyourcaree c3c4
cap lab var c3c4 "mentoring: friends"
cap ren neighborsdoyoutalktoaboutyourcar c3c5
cap lab var c3c5 "mentoring: neighbors" 
cap ren teachersorschoolcounselordoyouta c3c6
cap lab var c3c6 "mentoring: teachers school counselors"
cap ren othersdoyoutalktoaboutyourcareer c3c7 
cap lab var c3c7 "mentoring: others"

forval i=1/7 {
	cap replace c3c`i'="1" if ~missing(c3c`i')
	destring c3c`i', replace
	cap replace c3c`i'=0 if c3c`i'==.
}

* Knowledge about admission criteria of upper secondary school
cap ren artsstreamatgradexixiihowmuchdoy e1a 
cap lab var e1a "knowledge: admission Arts stream"
cap ren commercestreamatgradexixiihowmuc e1b 
cap lab var e1b "knowledge: admission Commerce stream"
cap ren sciencestreamatgradexixiihowmuch e1c 
cap lab var e1c "knowledge: admission Science stream"
cap ren rigzhungstreamatgradexixiihowmuc e1d 
cap lab var e1d "knowledge: admission Rigzhung stream"
cap ren ttiinsteadofgoingtogradexixiigen e1e
cap lab var e1e "knowledge: admission TTI"

* Knowledge about cateer paths  
cap ren v87 e4a
cap lab var e4a "knowledge: career Arts stream"
cap ren v88 e4b
cap lab var e4b "knowledge: career Commerce stream"
cap ren v89 e4c
cap lab var e4c "knowledge: career Science stream"
cap ren v90 e4d
cap lab var e4d "knowledge: career Rigzhung stream"
cap ren v91 e4e
cap lab var e4e "knowledge: career TTI stream"

* Degree ranking by genuine interest 
cap ren bachelorssciencemathcomputerscie e6a
cap lab var e6a "ranking: SCI"
cap tostring e6a, replace 
cap ren bachelorsengineeringpleasechoose e6b
cap lab var e6b "ranking: ENG"
cap tostring e6b, replace 
cap ren bachelorsmedicinehealthandnursin e6c
cap lab var e6c "ranking: MED"
cap tostring e6c, replace 
cap ren bachelorseducationpleasechoosean e6d
cap lab var e6d "ranking: EDU"
cap tostring e6d, replace 
cap ren bachelorsbusinessmanagementecono e6e
cap lab var e6e "ranking: BUS"
cap tostring e6e, replace 
cap ren bachelorsartshumanitiesandsocial e6f
cap lab var e6f "ranking: ARTS"
cap tostring e6f, replace 
cap ren technicaldegreesnationalcertific e6g
cap lab var e6g "ranking: TTI"
cap tostring e6g, replace 
cap ren stopeducationafterhighschoolplea e6h
cap lab var e6h "ranking: HighSchool"
cap tostring e6h, replace 

* Based on academic performance, difficulty
cap ren basedonyouracademicperformanceho e7c
cap lab var e7c "how difficult to enroll in Science stream"
cap ren v106 e8c
cap lab var e8c "how difficult to complete Science stream"

* How much will enjoy each stream 
cap ren artsstreamgradexixiihowmuchdoyou e9a
cap lab var e9a "enjoyability: Arts stream"
cap ren commercestreamgradexixiihowmuchd e9b
cap lab var e9b "enjoyability: Commerce stream"
cap ren sciencestreamgradexixiihowmuchdo e9c
cap lab var e9c "enjoyability: Science stream"
cap ren rigzhungstreamgradexixiihowmuchd e9d
cap lab var e9d "enjoyability: Rigzhung stream"
cap ren ttieducationaftersecondaryschool e9e
cap lab var e9e "enjoyability: TTI stream"

* Approval 
cap ren howlikelydoyouthinkthatyourparen e11c
cap lab var e11c "Approval of parents if choosing Science stream"
cap ren howlikelydoyouthinkthatyourfrien e12c
cap lab var e12c "Approval of friends if choosing Science stream"
cap ren howlikelydoyouthinkpeopleingener e13c
cap lab var e13c "Approval of society if choosing Science stream"

* Decision today
cap ren artsstreamgradexixiiifyoutodayde e14a 
cap tostring e14a, replace 
cap lab var e14a "ranking: Arts stream"
cap ren commercestreamgradexixiiifyoutod e14b
cap tostring e14b, replace 
cap lab var e14b "ranking: Commerce stream"
cap ren sciencestreamgradexixiiifyoutoda e14c
cap tostring e14c, replace 
cap lab var e14c "ranking: Science stream"
cap ren rigzhungstreamgradexixiiifyoutod e14d
cap tostring e14d, replace 
cap lab var e14d "ranking: Rigzhung stream"
cap ren v114 e14e
cap tostring e14e, replace 
cap lab var e14e "ranking: TTI"
cap ren stopeducationaftersecondaryschoo e14f
cap tostring e14f, replace 
cap lab var e14f "ranking: MiddleSchool"

* Sure about e14?
cap ren howsureareyouaboutyourchoicesand e15
cap lab var e15 "How sure about e14"

* Beliefs about % of other students choice
cap ren artsstreamgradexixiiwhatpercenta e16a_m
cap lab var e16a_m "belief: % male students choosing Arts"
cap ren commercestreamgradexixiiwhatperc e16b_m
cap lab var e16b_m "belief: % male students choosing Commerce"
cap ren sciencestreamgradexixiiwhatperce e16c_m
cap lab var e16c_m "belief: % male students choosing Science"
cap ren rigzhungstreamgradexixiiwhatperc e16d_m
cap lab var e16d_m "belief: % male students choosing Rigzhung"
cap ren v121 e16e_m
cap lab var e16e_m "belief: % male students choosing TTI"
cap ren v122 e16f_m
cap lab var e16f_m "belief: % male students stop edu after"
cap ren v123 e16a_f
cap lab var e16a_f "belief: % female students choosing Arts"
cap ren v124 e16b_f
cap lab var e16b_f "belief: % female students choosing Commerce"
cap ren v125 e16c_f
cap lab var e16c_f "belief: % female students choosing Science"
cap ren v126 e16d_f
cap lab var e16d_f "belief: % female students choosing Rigzhung"
cap ren v127 e16e_f
cap lab var e16e_f "belief: % female students choosing TTI"
cap ren v128 e16f_f
cap lab var e16f_f "belief: % female students stop edu after"

* Education plans
cap ren iftherewerenobarriershowfarinsch e17
cap lab var e17 "education plan if no barriers"
cap ren asthingsstandnowhowfarinschooldo e18 
cap lab var e18 "education plan as things stand now"

* Personalities 
cap ren isreservedhowmuchdoyouagreeordis h1a
cap ren tendstobelazyhowmuchdoyouagreeor h1b 
cap ren isoutgoingsociablehowmuchdoyouag h1c 
cap ren doesathoroughjobhowmuchdoyouagre h1d 
cap ren getsnervouseasilyhowmuchdoyouagr h1e 
cap ren hasanactiveimaginationhowmuchdoy h1f 
cap ren worriesalothowmuchdoyouagreeordi h1g 
cap ren remainscalmintensesituationshowm h1h 
cap ren istalkativehowmuchdoyouagreeordi h1i 
cap ren isoriginalcomesupwithnewideashow h1j 
cap ren valuesartisticaestheticexperienc h1k 
cap ren issometimesrudetoothershowmuchdo h1l 
cap ren hasaforgivingnaturehowmuchdoyoua h1m 
cap ren isconsiderateandkindtoalmostever h1n 
cap ren doesthingsefficientlyhowmuchdoyo h1o 

* Family background 
cap ren whatisthehighestlevelofeducation x3 
cap lab var x3 "father education"
cap ren v147 x7 
cap lab var x7 "mother education"

* Wealth
cap ren landplotdoyourparentsandotherhou x9a
cap lab var x9a "wealth: land plot"
cap ren housebuildingdoyourparentsandoth x9b
cap lab var x9b "wealth: house building"
cap ren vehiclenoncommercialdoyourparent x9c
cap lab var x9c "wealth: noncommercial vehicle"
cap ren motorcycledoyourparentsandotherh x9d
cap lab var x9d "wealth: motorcycle"
cap ren refrigeratordoyourparentsandothe x9e
cap lab var x9e "wealth: fridge"
cap ren washingmachinedoyourparentsandot x9f
cap lab var x9f "wealth: washing machine"
cap ren tvdoyourparentsandotherhousehold x9g
cap lab var x9g "wealth: tv"
cap ren mobilephonedoyourparentsandother x9h
cap lab var x9h "wealth: mobilephone"
cap ren computerdesktoporlaptopdoyourpar x9i
cap lab var x9i "wealth: computer"

* Siblings
cap ren howmanysiblingsdoyouhave x10
cap lab var x10 "siblings: total number"
cap destring x10, replace
cap ren howmanyofyoursiblingsarecurrentl x11a
cap lab var x11a "siblings: Science stream/STEM"
cap destring x11a, replace
cap ren v159 x11b
cap lab var x11b "siblings: non-Science stream/non-STEM"
cap destring x11b, replace
cap ren v160 x11c 
cap lab var x11c "siblings: high school or above"
cap destring x11c, replace

* Relatives
cap ren howmanyofyourrelativesexcludingp x12a
cap lab var x12a "relatives: Science stream/STEM"
cap destring x12a, replace
cap ren v162 x12b
cap lab var x12b "relatives: non-Science stream/non-STEM"
cap destring x12b, replace
cap ren v163 x12c
cap lab var x12c "relatives: high school or above"
cap destring x12c, replace

* Friends
cap ren nameoffriendspleasewritedownthen y1a1
cap lab var y1a1 "friend 1: name"
cap ren v165 y1b1 
cap lab var y1b1 "friend 2: name"
cap ren v166 y1c1
cap lab var y1c1 "friend 3: name"
format y1*1 %30s

cap ren phonenumberpleasewritedownthenam y1a2
cap lab var y1a2 "friend 1: phone"
cap ren v168 y1b2
cap lab var y1b2 "friend 2: phone"
cap ren v169 y1c2
cap lab var y1c2 "friend 3: phone"
format y1*2 %10s

cap ren emailaddressespleasewritedownthe y1a3
cap lab var y1a3 "friend 1: email"
cap ren v171 y1b3
cap lab var y1b3 "friend 2: email"
cap ren v172 y1c3
cap lab var y1c3 "friend 3: email"
format y1*3 %30s

* Hobbies
cap ren listentomusichowmuchdoyoulikethe z1a
cap lab var z1a "hobby: listen to music" 
cap ren playmusichowmuchdoyoulikethefoll z1b 
cap lab var z1b "hobby: play music"
cap ren watchsportshowmuchdoyoulikethefo z1c
cap lab var z1c "hobby: watch sports"
cap ren playsportshowmuchdoyoulikethefol z1d
cap lab var z1d "hobby: play sports"
cap ren watchtvhowmuchdoyoulikethefollow z1e
cap lab var z1e "hobby: watch tv"
cap ren playvideoandonlinegameshowmuchdo z1f
cap lab var z1f "hobby: play videoonline games"

* Codes shared by Checku to correct school names
cap ren pleasespecifythenameofyoursecond other_school

cap foreach var of varlist other_school {
cap replace `var'= trim(itrim(lower(`var')))  // reduce multiple & consecutive blanks/make words into lower case
cap replace `var'= subinstr(`var'," ","",.)   //Removes spacing between words
cap replace `var'= subinstr(`var',",","",.)   //Removes comma (,)
cap replace `var'= subinstr(`var',"'","",.)   //Removes apostrophe (')
cap replace `var'= subinstr(`var',".","",.)   //Removes dot (.) 
cap replace `var'= subinstr(`var',"/","",.)   //Removes slash (/)
cap replace `var'= subinstr(`var',"-","",.)   //Removes dash (-)
cap replace `var'= subinstr(`var',"(","",.)   //Removes opening parentheses
cap replace `var'= subinstr(`var',")","",.)   //Removes closing parentheses
cap }


* For August 19
cap replace other_school="Babesa Higher Secondary School" if regexm(other_school,"babesa|babea|bhabe")
cap replace other_school="Changangkha Middle Secondary School" if regexm(other_school,"changang")
cap replace other_school="Changzamtog Middle Secondary School" if regexm(other_school,"changzam|chgangzam|channgzam|chgangzm|chnagzam|cnahgzam")
cap replace other_school="Chapcha Middle Secondary School" if regexm(other_school,"chapcham")
cap replace other_school="Choekhorling Middle Secondary School" if regexm(other_school,"choekho|chokhor")
cap replace other_school="Zilukha Middle Secondary School" if regexm(other_school,"zil|zlu")
cap replace other_school="Yangchenphug Higher Secondary School" if regexm(other_school,"yangchen|yanchen|yhss")
cap replace other_school="Samdrupjongkhar Middle Secondary School" if regexm(other_school,"samdrup")
cap replace other_school="Bajo Higher Secondary School" if regexm(other_school, "baj")
cap replace other_school="Bartsham Central School" if regexm(other_school, "bart")
cap replace other_school="Bitekha Middle Secondary School" if regexm(other_school,"bite")
cap replace other_school="Bjishong Central School" if regexm(other_school,"bjis|bjs")
cap replace other_school="Buli Central School" if regexm(other_school,"buli")
cap replace other_school="Chapcha Middle Secondary School" if regexm(other_school,"chacha")
cap replace other_school="Chukha Central School" if regexm(other_school, "chukha|chhukha")
cap replace other_school="Chukha Central School" if regexm(other_school,"chuukha")
cap replace other_school="Chaskhar Central School" if regexm(other_school,"chas")
cap replace other_school="Chhumey Central School" if regexm(other_school,"chhumey|chumey|chummy")
cap replace other_school="Chumigthang Middle Secondary School" if regexm(other_school,"chumig")
cap replace other_school="Chundu Armed Forces Public Central School" if regexm(other_school,"chund")
cap replace other_school="Daga Central School" if regexm(other_school,"dadacs|dagac|dagah|dagan")
cap replace other_school="Dagapela Midde Secondary School" if regexm(other_school,"dagape")
cap replace other_school="Damphu Central School" if regexm(other_school,"damphuc")
cap replace other_school="Dashiding Higher Secondary School" if regexm(other_school,"dashi")
cap replace other_school="Dechencholing Higher Secondary School" if regexm(other_school,"dechench|dechgen|dexhen")
cap replace other_school="Desi Higher Secondary School" if regexm(other_school,"desi|deso|dessi")
cap replace other_school="Dashiding Higher Secondary School" if regexm(other_school,"dhas")
cap replace other_school="Dorokha Central School" if regexm(other_school,"doro")
cap replace other_school="Dr. Tobgyel School" if regexm(other_school,"drt|doctor")
cap replace other_school="Drukgyel Central School" if regexm(other_school,"drug|drukg")
cap replace other_school="Drukjegang Central School" if regexm(other_school,"drukj|druje")
cap replace other_school="Druk School" if regexm(other_school,"drukschool")
cap replace other_school="Dungsam Academy" if regexm(other_school,"dungs")
cap replace other_school="Dungsam Academy" if regexm(other_school,"dungs")
cap replace other_school="Bjishong Central School" if regexm(other_school,"bjish|bjs")
cap replace other_school="Chapcha Middle Secondary School" if regexm(other_school,"chapcha")
cap replace other_school="Garpawoong Middle Secondary School" if regexm(other_school,"garpa|garpo")
cap replace other_school="Gaselo Central School" if regexm(other_school,"gaselo")
cap replace other_school="Gelephu Higher Secondary School" if regexm(other_school,"gelephuh")
cap replace other_school="Wangbama Central School" if regexm(other_school,"genekha")
cap replace other_school="Gesarling Central School" if regexm(other_school,"gesar")
cap replace other_school="Gomdar Central School" if regexm(other_school, "gomd")
cap replace other_school="Gomtu Higher Secondary School" if regexm(other_school, "gomtu")
cap replace other_school="Gongthung Middle Secondary School" if regexm(other_school, "gongt")
cap replace other_school="Gongzim Ugyen Dorji Central School" if regexm(other_school, "gongz")
cap replace other_school="Gyelpozhing Central School" if regexm(other_school, "gyelpo|gyalpo")
cap replace other_school="Jakar Higher Secondary School" if regexm(other_school, "hakar|jaj|jakarh")
cap replace other_school="Jampel Higher Secondary School" if regexm(other_school, "jampel")
cap replace other_school="Jangchubling Middle Secondary" if regexm(other_school, "jangchub")
cap replace other_school="Jigmecholing Middle Secondary School" if regexm(other_school, "jigmechoe")
cap replace other_school="Jigme Sherubling Central School" if regexm(other_school, "jigmeshe")
cap replace other_school="Jigme Sherubling Central School" if regexm(other_school, "jlhamoyz")
cap replace other_school="Jomotsangkha Middle Secondary School" if regexm(other_school, "jomotsa")
cap replace other_school="Jomotsangkha Middle Secondary School" if regexm(other_school, "jomotsha")
cap replace other_school="Kabesa Central School" if regexm(other_school, "kabesa")
cap replace other_school="Kamji Central School" if regexm(other_school, "kamji")
cap replace other_school="Karma Academy" if regexm(other_school, "karmaacademy|karmaa|kramaacademy")
cap replace other_school="Karmaling Higher Secondary School" if regexm(other_school,"karmlinghighersecondaryschool")
cap replace other_school="Karmaling Higher Secondary School" if regexm(other_school, "karmaling")
cap replace other_school="Kelki Higher Secondary School" if regexm(other_school, "kelki")
cap replace other_school="Kuendrup Higher Secondary School" if regexm(other_school, "kendrup")
cap replace other_school="Kengkhar Middle Secondary School" if regexm(other_school, "kengkhar")
cap replace other_school="Khasadrapchu Middle Secondary School" if regexm(other_school, "khada")
cap replace other_school="Arekha Middle Secondary School" if regexm(other_school, "arekha")
cap replace other_school="Changangkha Middle Secondary School" if regexm(other_school, "changangkha")
cap replace other_school="Changzamtog Middle Secondary School" if regexm(other_school, "changzamto")
cap replace other_school="Chokhorling Middle Secondary School" if regexm(other_school, "choeckhor|chokhor")
cap replace other_school="Chukha Central School" if regexm(other_school, "chukh")
cap replace other_school="Chukha Central School" if regexm(other_school, "chukh|chukkh")
cap replace other_school="Chhumey Central School" if regexm(other_school, "chummey")
cap replace other_school="Damphu Middle Secondary School" if regexm(other_school, "damphu")
cap replace other_school="Darla Middle Secondary School" if regexm(other_school, "darla")
cap replace other_school="Dashiding Higher Secondary School" if regexm(other_school, "dasguding")
cap replace other_school="Dechentsemo Central School" if regexm(other_school, "dechentse")
cap replace other_school="Chukha Central School" if regexm(other_school, "chuckha")
cap replace other_school="Dechentsemo Central School" if regexm(other_school, "dechentshemo")
cap replace other_school="Drametse Central School" if regexm(other_school, "drametse")
cap replace other_school="Dungtse Central School" if regexm(other_school, "dungtse")
cap replace other_school="Dungtse Central School" if regexm(other_school, "dungtshe")
cap replace other_school="Gongzim Ugyen Dorji Central School" if regexm(other_school,"gonzim")
cap replace other_school="Gyelpozhing Central School" if regexm(other_school,"gyei")
cap replace other_school="Jakar Higher Secondary School" if regexm(other_school,"jakar|jaker|jakhar")
cap replace other_school="Jampel Higher Secondary School" if regexm(other_school,"jamp")
cap replace other_school="Jigme Sherubling Central School" if regexm(other_school,"jimesherublingcentralschool")
cap replace other_school="Loseling Middle Secondary School" if regexm(other_school,"loselingmiddlesecondaryschool|loselingsecondaryschool|loslingmiddlesecondaryschool|losselingmiddlesecondaryschool")
cap replace other_school="Motithang Higher Secondary School" if regexm(other_school,"moithanghighersecondaryschool|mhss|motithang| motitihanghss")
cap replace other_school="Khangkhu Middle Secondary School" if regexm(other_school,"khangkhu")
cap replace other_school="Khasadrapchu Middle Secondary School" if regexm(other_school,"khasadrap")
cap replace other_school="Khuruthang Middle Secondary School" if regexm(other_school,"khuruthang")
cap replace other_school="Kuendrup Higher Secondary School" if regexm(other_school,"kuendrup|kuendryp|kundrup")
cap replace other_school="Lamgong Middle Secondary School" if regexm(other_school,"lamgong")
cap replace other_school="Kuzhugchen Middle Secondary School" if regexm(other_school,"kuzhugchenmiddlesecondaryschool")
cap replace other_school="Laya Central School" if regexm(other_school,"layacentral")
cap replace other_school="Lhamoyzingkha Central School" if regexm(other_school,"lhamo")
cap replace other_school="Losel Gyatsho Academy" if regexm(other_school,"losalgy|losealgy|loselgy")
cap replace other_school="Loseling Middle Secondary School" if regexm(other_school,"loselling|losling|loslling")
cap replace other_school="Losel Gyatsho Academy" if regexm(other_school,"loseljya|lpselgy")
cap replace other_school="Kamji Central School" if regexm(other_school,"kami")
cap replace other_school="Kunzangling Central School" if regexm(other_school,"kunzang")
cap replace other_school="Lungtenphu Middle Secondary School" if regexm(other_school,"lungtenp|luntenphum")
cap replace other_school="Lungtenzampa Middle Secondary School" if regexm(other_school,"lungtenz|luentenzam|lyngten")
cap replace other_school="Martshala Central School" if regexm(other_school,"martshala")
cap replace other_school="Mendregang Central School" if regexm(other_school,"mendral")
cap replace other_school="Mendregang Central School" if regexm(other_school,"mendreal")
cap replace other_school="Mendregang Central School" if regexm(other_school,"mendreg")
cap replace other_school="Mendregang Central School" if regexm(other_school,"mendrelgang")
cap replace other_school="Mendregang Central School" if regexm(other_school,"mendrelga")
cap replace other_school="Minjiwoong Central School" if regexm(other_school,"miji|minji")
cap replace other_school="Mongar Higher Secondary School" if regexm(other_school,"mongarh")
cap replace other_school="Mongar Middle Secondary School" if regexm(other_school,"mongarm|mongarsec")
cap replace other_school="Mongar Higher Secondary School" if regexm(other_school,"monggarh|mongerh")
cap replace other_school="Mongar Sherub Reldri Higher Secondary School" if regexm(other_school,"mongarsher")
cap replace other_school="Motithang Higher Secondary School" if regexm(other_school,"mothithanghi|mothithangse|motitanghi|motithanghi|motithanghss")
cap replace other_school="Nagor Middle Secondary School" if regexm(other_school,"nador|nagor")
cap replace other_school="Nangkor Central School" if regexm(other_school,"nangkor")
cap replace other_school="Gelephu Higher Secondary School" if regexm(other_school,"gelephumid")
cap replace other_school="Nganglam Central School" if regexm(other_school,"nganglamc"|)
cap replace other_school="Nganglam Central School" if regexm(other_school,"ngagl"|)
cap replace other_school="Nganglam Middle Secondary School" if regexm(other_school,"ngangalmh"|)
cap replace other_school="Nima Higher Secondary Shool" if regexm(other_school,"nimah"|)
cap replace other_school="Nima Higher Secondary Shool" if regexm(other_school,"nima"|)
cap replace other_school="Nima Higher Secondary Shool" if regexm(other_school,"norbua"|)
cap replace other_school="Norbugang Central School" if regexm(other_school,"norbuga"|)
cap replace other_school="Orong Central School" if regexm(other_school,"orong"|)
cap replace other_school="Nganglam Central School" if regexm(other_school,"nganglanc"|)
cap replace other_school="Norbugang Central School" if regexm(other_school,"norb"|)
cap replace other_school="Motithang Higher Secondary School" if regexm(other_school,"motithngs|motithse|motiththang"|)
cap replace other_school="Nganglam Middle Secondary School" if regexm(other_school,"nganglamm"|)
cap replace other_school="Pakshika Central School" if regexm(other_school,"paksh|pashik"|)
cap replace other_school="Pelkhil Higher Secondary School" if regexm(other_school,"pekhil|pelhkil|pelikh|pelkhail|pelkhel|pelkhi|pelki"|)
cap replace other_school="Losel Gyatsho Academy" if regexm(other_school,"loselgaytshoacadmey|losegya")
cap replace other_school="Pakshika Central School " if regexm(other_school,"pakishakhacentralschool")
cap replace other_school="Peljorling Higher Secondary School" if regexm(other_school,"pelj")
cap replace other_school="Pelrithang Higher Secondary School" if regexm(other_school,"pelri")
cap replace other_school="Phuentsholing Middle Secondary School" if regexm(other_school,"phuentshol")
cap replace other_school="Phobjikha Central School" if regexm(other_school,"phobji")
cap replace other_school="Phuntshothang Middle Secondary School" if regexm(other_school,"phuntshothang")
cap replace other_school="Phuentsholing Middle Secondary School" if regexm(other_school,"phuntshol")
cap replace other_school="Punakha Central School" if regexm(other_school,"punak|punkha")
cap replace other_school="Radi Middle Secondary School" if regexm(other_school,"radhi")
cap replace other_school="Rangjung Central School" if regexm(other_school,"rangj")
cap replace other_school="Rangjung Central School" if regexm(other_school,"ranj")
cap replace other_school="Rinchen  Higher Secondary School" if regexm(other_school,"renchen|richen|rinchen")
cap replace other_school="Rigzom Academy" if regexm(other_school,"rig")
cap replace other_school="Samcholing Higher Secondary School" if regexm(other_school,"samcho")
cap replace other_school="Ramdrupjongkhar Middle Secondary School" if regexm(other_school,"samdrupj")
cap replace other_school="Rinchen  Higher Secondary School" if regexm(other_school,"rinche")
cap replace other_school="Pemagatshel Middle Secondary School" if regexm(other_school,"pema")
cap replace other_school="Samtengang Central School" if regexm(other_school,"samten")
cap replace other_school="Samtse Higher Secondary School" if regexm(other_school,"samtseh|samtses")
cap replace other_school="Sarpang Central School" if regexm(other_school,"sapangc|sarpangc")
cap replace other_school="Sarpang Higher Secondary School" if regexm(other_school,"sapangh|sarpangh")
cap replace other_school="Sarpang Middle Secondary School" if regexm(other_school,"sarpangmiddlesecondaryschool")
cap replace other_school="Shaba Higher Secondary School" if regexm(other_school,"shaba")
cap replace other_school="Shaba Higher Secondary School" if regexm(other_school,"shabhi")
cap replace other_school="Mongar Sherub Reldri Higher Secondary School" if regexm(other_school,"sharab|sherabrd|sherabreldhi|sherabreldihss|sherabreldri|sherabreldrihighersecondaryschoolsherubreldhihss|sherubreldihighersecondaryschool|sherubreldisecondaryschool|sherubreldri|sherubreldrihighersecondaryschool|sherubreldrihighsecondaryschool|sherubreldrisecondaryschool")
cap replace other_school="Shari Hugher Secondary School" if regexm(other_school,"shari|sshari")
cap replace other_school="Samcholing Higher Secondary School" if regexm(other_school,"shemcholinghighersecondryschool")
cap replace other_school="Sonamgang Middle Secondary School" if regexm(other_school,"sonamg")
cap replace other_school="Sonam Kuenphen Higher Secondary School" if regexm(other_school,"sonamk")
cap replace other_school="SonamThang Central School" if regexm(other_school,"sonamthang")
cap replace other_school="Sherubling Central School" if regexm(other_school,"sherublingcentralschool")
cap replace other_school="Samtse Higher Secondary School" if regexm(other_school,"smatsehighersecondaryschool")
cap replace other_school="Tashitse Higher Secondary School" if regexm(other_school,"taahitsehighersecondaryschool| tashistehighersecondaryschool|tashitse|tashitestashist")
cap replace other_school="Taktse Central School" if regexm(other_school,"taktse")
cap replace other_school="Mongar Sherub Reldri Higher Secondary Schooll" if regexm(other_school,"sherubreldhihss")
cap replace other_school="Tang Central School" if regexm(other_school,"tang")
cap replace other_school="Tashitse Higher Secondary School" if regexm(other_school,"tashiste|tashitshe")
cap replace other_school="Tashidingkha Central School" if regexm(other_school,"tashidingkhacentralschool")
cap replace other_school="Trashigang Middle Secondary School" if regexm(other_school,"tashigangm")
cap replace other_school="Tashitse Higher Secondary School" if regexm(other_school,"tashitdse|tashites")
cap replace other_school="Tendruk Central School" if regexm(other_school,"tendeuk|tendruktenduek|tendurk")
cap replace other_school="Tendruk Central School" if regexm(other_school,"tendeuk|tendruk|tenduek|tendurk")
cap replace other_school="Tendruk Central School" if regexm(other_school," tendrku|tendru|tendruikcentral")
cap replace other_school="Thrimshing Central School" if regexm(other_school,"thrimshingcentralschool")
cap replace other_school="Trashigang Middle Secondary School" if regexm(other_school,"trashigangmiddlesecondaryschool")
cap replace other_school="Tendruk Central School" if regexm(other_school,"tendrku")
cap replace other_school="Thrimshing Central School" if regexm(other_school,"thrimshing")
cap replace other_school="Tashitse Higher Secondary School" if regexm(other_school,"trashitsehighersecondaryschool")
cap replace other_school="Tashitse Higher Secondary School" if regexm(other_school,"tashihighersecondaryschool")
cap replace other_school="Tshangkha Central School" if regexm(other_school,"tsangkhacentralschool|tshangkha|tshngkha")
cap replace other_school="Tsenkharla Central School" if regexm(other_school,"tsenkharlacentralschool")
cap replace other_school="Tsimalakha Middle  Secondary School" if regexm(other_school,"tshimalhakhalowersecondaryschool")
cap replace other_school="Tshangkha Central School" if regexm(other_school,"tshongkhagsecondaryschool")
cap replace other_school="Tsirang Toe Central School" if regexm(other_school,"tsirangtoecentalschool|tsirangtoecentralschool")
cap replace other_school="Tendruk Central School" if regexm(other_school,"twndrukcentralschool")
cap replace other_school="Gongzim Ugyen Dorji Central School" if regexm(other_school,"ugyendorjicentralschool")
cap replace other_school="Ura Central School" if regexm(other_school,"ura")
cap replace other_school="UTPAL Academy" if regexm(other_school,"utpal")
cap replace other_school="Wangbama Central School" if regexm(other_school,"wangbama")
cap replace other_school="Wangchu Middle Secondary School" if regexm(other_school," wangchumiddlesecondaryschool|wangchumss")
cap replace other_school="Wangchu Middle Secondary School" if regexm(other_school,"wangchumiddlesecondaryschool|wangchumss")
cap replace other_school="Yadi Central School" if regexm(other_school,"yadi|yadhi")
cap replace other_school="Yangchenphug Higher Secondary School" if regexm(other_school,"yangchen")
cap replace other_school="Yangchenphug Higher Secondary School" if regexm(other_school,"yanchengphughighersecondaryschool|yanchenphughighersecondaryschool|yandchenphughighersecondaryschool|yang|yangch|yangche")
cap replace other_school="Yebilaptsa Central School" if regexm(other_school,"yebi")
cap replace other_school="Yelchen Central School" if regexm(other_school,"yelchencentralschool")
cap replace other_school="Yonten Kuenjung Academy" if regexm(other_school,"yentenkungung|yentonkuenjungacademy|yeonteonkuenjung|yoenten|yountenk|yontenkuen|younten|yonten|yout|youent")
cap replace other_school="Yonten Kuenjung Academy" if regexm(other_school,"yoeutenkuenjungacadamey|yontenkenjungacademy|yontenkunejungacademy|yontenkunjungacademy|youentenkenzungacademy|youtenkuenjungacademy")
cap replace other_school="Yoezerling Higher Secondary School" if regexm(other_school,"yeozerlinghighersecondaryschool| yoezerlinghighersecondaryschool")
cap replace other_school="Yoezerling Higher Secondary School" if regexm(other_school,"yeozerlinghighersecondaryschool|yoezerlinghighersecondaryschool")
cap replace other_school="Yoeseltse Middle Secondary School" if regexm(other_school,"yoeseltsemiddlesecondaryschool")
cap replace other_school="Zhemgang Central School" if regexm(other_school,"zhamg|zhemg")
cap replace other_school="Zhemgang Central School" if regexm(other_school,"zhemagagcentralschool|zhem")
cap replace other_school="Yangchenphug Higher Secondary School" if regexm(other_school,"yhss")
cap replace other_school="Zilukha Middle Secondary School" if regexm(other_school,"zilukhamiddlesecondaryschool|zilukhamss|zilu")
cap replace other_school="Yangchenphug Higher Secondary School" if regexm(other_school,"yengchenphughighersecondaryschool")
cap replace other_school="Ugyen Academy" if regexm(other_school,"ugyenacademy")
cap replace other_school="Changzamtog Middle Secondary School" if regexm(other_school,"chagzamtogmiddelsecoundaryschool|chamzamtogmiddlesecondaryschool")
cap replace other_school="Dechentsemo Central School" if regexm(other_school,"dechentshenmohigherschool")
cap replace other_school="ELC High School" if regexm(other_school,"elc|elch|elchigh|elchighersecondaryschool|elchighschool|elchighsecondaryschool")
cap replace other_school="Jakar Higher Secondary School" if regexm(other_school,"jarkarhighersecondayschool|jaka")
cap replace other_school="Samtse Higher Secondary School" if regexm(other_school,"samstehighersecondaryschool|samtse|samtsegi")
cap replace other_school="Yonten Kuenjung Academy" if regexm(other_school,"yentenkunjungacademy|yontuenkuenjungacademy|yoten")
cap replace other_school="Yurung Central School" if regexm(other_school,"yurungcentralschool")
cap replace other_school="Druk School" if regexm(other_school,"druk")

* For August 17
cap replace other_school="Autsho Central School" if regexm(other_school,"autsho|atsho")
cap replace other_school="Bajo Higher Secondary School" if regexm(other_school, "baj|baho|bacon")
cap replace other_school="Baylling Central School" if regexm(other_school, "bally|bay")
cap replace other_school="Bjishong Central School" if regexm(other_school,"bjis|bjs")
cap replace other_school="Chapcha Middle Secondary School" if regexm(other_school,"chapcham")
cap replace other_school="Jampel Higher Secondary School" if regexm(other_school, "jampel")
cap replace other_school="Babesa Higher Secondary School" if regexm(other_school,"babesa|babea|bhabe")
cap replace other_school="Changangkha Middle Secondary School" if regexm(other_school,"changang")
cap replace other_school="Changzamtog Middle Secondary School" if regexm(other_school,"changzam|chgangzam|channgzam|chgangzm|chnagzam|cnahgzam")
cap replace other_school="Gedu Higher Secondary School" if regexm(other_school,"gedu|ggedu")
cap replace other_school="Choekhorling Middle Secondary School" if regexm(other_school,"choekho|chokhor")
cap replace other_school="Zilukha Middle Secondary School" if regexm(other_school,"zil|zlu")
cap replace other_school="Yangchenphug Higher Secondary School" if regexm(other_school,"yangchen|yanchen|yhss")
cap replace other_school="Samdrupjongkhar Middle Secondary School" if regexm(other_school,"samdrup")
cap replace other_school="Kabesa Central School" if regexm(other_school, "kabesa|kebesa")
cap replace other_school="Kamji Central School" if regexm(other_school, "kamji")
cap replace other_school="Kidheykhar Central School" if regexm(other_school, "kidh")
cap replace other_school="Khuruthang Middle Secondary School" if regexm(other_school,"khurutha|kuruthang")
cap replace other_school="Kunzangling Central School" if regexm(other_school,"kunzang|kuenzang|kunzling")
cap replace other_school="Lhuentse Higher Secondary School" if regexm(other_school,"lhuntse|lhuents")
cap replace other_school="Minjey Middle Secondary School" if regexm(other_school,"miinj|minj")
cap replace other_school="Ramjar Middle Secondary School" if regexm(other_school,"ramj")
cap replace other_school="Samtengang Central School" if regexm(other_school,"samten")
cap replace other_school="Tangmachu Central School" if regexm(other_school,"tangm")
cap replace other_school="Tashidingkha Central School" if regexm(other_school,"tashiding|tashiingkha")	
cap replace other_school="Tsenkharla Central School" if regexm(other_school,"tsen")	

* School name variable
forval i=13/35 {
	cap tostring v`i', replace
	cap replace v`i'="" if v`i'=="."
	cap replace schoolname=v`i' if (missing(schoolname)|schoolname=="Other (Specify)") & ~missing(v`i') & v`i'~="Other (Specify)"
}
cap replace schoolname = other_school if missing(schoolname) | schoolname=="Other (Specify)"

* Drop irrelevant variables
cap drop status onlinesurveyoncareereducationfor thissurveyisintendedforgradeixor v*
cap drop reenteryourstudentidcode reenteryouremailaddress reenteryourmobilephonenumber

end

********************************************************************************
*	 Apply the program to survey data and save as new datasets
*
********************************************************************************
import delimited "$raw/student/grade910/20210918121007-SurveyExport.csv", clear
clean_grade910
save "$temp/grade910_part1.dta", replace

import delimited "$raw/student/grade910/20210918121207-SurveyExport.csv", clear
clean_grade910
save "$temp/grade910_part2.dta", replace

* Pilot school 
import delimited "$raw/student/grade910/20210918121805-SurveyExport-Pilot.csv", clear
clean_grade910
rename dzongkhagofyourschoolwhatisthena district
rename schoolwhatisthenameofyourseconda schoolname
rename whatisyouremailaddressprovidedby email 
rename whatisyourprivateemailaddress email2
drop reenteryourstudentidcode reenteryourprivateemailaddress reenteryourmobilephonenumber doyouliveinaschooldormitory itisimportantformetoreceiveeduca educationmakesmeabettercitizenho educationhelpsmegetabetterjobhow becauseofscienceandtechnologythe itisnotimportantformetoknowabout howstronglydoyouassociatescience ifwomenhavestemsciencetechnology stemrelatedjobsareformenthanwome womenarelesswillingtodevotetimer parentsdoyoutalktoaboutyourperso friendsdoyoutalktoaboutyourperso neighborsdoyoutalktoaboutyourper othersdoyoutalktoaboutyourperson parentsdoyoutalktoaboutyouractiv friendsdoyoutalktoaboutyouractiv neighborsdoyoutalktoaboutyouract othersdoyoutalktoaboutyouractivi incomeforyourfutureeducationalde mygenuineinterestforyourfutureed advicefromparentsorguardiansfory whatmyfriendsthinkandchooseforyo agricultureforestryandfishingwhi miningwhichsectorswouldyouprefer manufacturingwhichsectorswouldyo electricitygaswatersupplywastema constructionwhichsectorswouldyou retailwholesaletradingwhichsecto hotelrestaurantfoodserviceswhich transportationwhichsectorswouldy informationandcommunicationwhich financialandinsuranceactivitiesw educationwhichsectorswouldyoupre healthwhichsectorswouldyouprefer entertainmentartsrecreationwhich professionalscientificandtechnic iknowexactlywhichoccupationiwant artsstreamgradexixiipleaserankth commercestreamgradexixiipleasera sciencestreamgradexixiipleaseran rigzhungstreamgradexixiipleasera iamnotinterestedincontinuingeduc bachelorsengineeringpleaserankth bachelorseducationpleaserankthef artsstreamgradexixiibasedonyoura commercestreamgradexixiibasedony sciencestreamgradexixiibasedonyo rigzhungstreamgradexixiibasedony artsstreamgradexixiiimaginethaty commercestreamgradexixiiimaginet sciencestreamgradexixiiimagineth rigzhungstreamgradexixiiimaginet ifyouhaveachancetomeetsomeonewho isyourfatheralive doyoucurrentlylivewithyourfather whichofthefollowingthebasedescri otherpleasedescribewhichofthefol isyourmotheralive doyoucurrentlylivewithyourmother
save "$temp/grade910_part3.dta", replace

* Put data together
clear
append using "$temp/grade910_part1.dta", force
g part=1
append using "$temp/grade910_part2.dta", force
replace part=2 if part==.
append using "$temp/grade910_part3.dta", force
replace part=3 if part==.

********** check if student observations are unique 

* duplicated submissions: based on studentid name b_day b_moth b_year
* keep the earliest submissions
duplicates tag studentid name b_day b_month b_year, g(dupsubmission)
sort studentid name b_day b_month b_year timestarted
g double surveytime =clock(timestarted, "MDY hms")
format surveytime %15.0f
bys studentid name b_day b_month b_year: egen double min=min(surveytime)
drop if min~=surveytime & dupsubmission~=0

drop min dupsubmission

* duplicated studentid 
duplicates tag studentid, g(temp)
tab temp

* drop duplicated observations 
drop if temp~=0

* Change name to lower cases 
cap replace name=strlower(name)
cap replace name=subinstr(name,"..","",.) 
cap replace name=subinstr(name,".","",.)   //Removes dot (.) 
cap replace name=subinstr(name,"  "," ",.)   //Removes double spaces
cap replace name=subinstr(name,"'","",.)   //Removes ' 
cap replace name=subinstr(name,"my name is ","",.)   //Removes my name is (.) 
cap replace name=subinstr(name,"my is ","",.)   //Removes my name is (.) 
cap replace schoolname=subinstr(schoolname,"  "," ",.)
cap replace schoolname=strlower(schoolname)
cap replace schoolname="dr tobgyel school" if schoolname=="dr. tobgyel school"
format name %30s
format schoolname %40s

drop if name=="test" | name==""
drop temp

* correct school names
replace schoolname="chokhorling middle secondary school" if schoolname=="choekhorling middle secondary school"
replace schoolname="jampel higher secondary school" if schoolname=="jampel higher secondary school "
replace schoolname="jangchubling middle secondary school" if schoolname=="jangchubling middle secondary"
replace schoolname="mongar middle secondary school" if schoolname=="mongar mss"

* correct district names 
rename district district_survey
merge m:1 schoolname using "$clean/list_of_schools.dta"
order district, b(district_survey)

tab schoolname if _merge==1
replace schoolname="" if _merge==1

drop if _merge==2
drop _merge 

save "$temp/grade910_baseline.dta", replace


*******************************************************************************
****** merge with grade information from list of student
* retrieve schoolname 
use "$temp/grade910_baseline.dta", clear 

preserve 
drop if missing(schoolname)
save "$clean/all_grade910_baseline.dta", replace 
restore 

keep if missing(schoolname)
merge 1:m studentid using "$temp/list_of_students"
keep if _merge==3
keep studentid schoolname2 grade responseid
rename grade grade0
save "$temp/grade910_baseline_part0.dta", replace


* matchID = studentid and schoolname 
use "$temp/list_of_students", clear
keep if inlist(grade, 9, 10)
rename schoolname2 schoolname 
unique studentid schoolname
merge 1:1 studentid schoolname using "$temp/grade910_baseline.dta"
drop stream2

preserve 
keep if _merge==3
keep studentid schoolname grade responseid
rename grade grade1
save "$temp/grade910_baseline_part1.dta", replace
restore 

* matchID = studentid 
preserve
drop if _merge==3 | _merge==2
keep studentid name2 grade schoolname 
duplicates tag studentid, g(temp)
list name2 schoolname if temp~=0
drop if temp~=0
rename schoolname schoolname2 

merge 1:1 studentid using "$temp/grade910_baseline.dta"
keep if _merge==3
drop schoolname 
rename schoolname2 schoolname 
keep studentid schoolname grade responseid
rename grade grade2
save "$temp/grade910_baseline_part2.dta", replace
restore 

* determine grade based on responseid for the remaining observations 
use "$temp/grade910_baseline.dta", clear 
merge 1:1 responseid studentid using "$temp/grade910_baseline_part0.dta"
replace schoolname=schoolname2 if missing(schoolname)
drop schoolname2
rename _merge m0
merge 1:1 responseid studentid using "$temp/grade910_baseline_part1.dta"
rename _merge m1
merge 1:1 responseid studentid using "$temp/grade910_baseline_part2.dta"
rename _merge m2

egen grade=rowmax(grade0 grade1 grade2)
drop grade0 grade1 grade2 
br schoolname responseid studentid grade part
format studentid %20s
sort schoolname responseid part

replace schoolname="dr tobgyel school" if other_school=="Dr. Tobgyel School" & missing(schoolname)

g grade_list = grade 

forval i=1/50 {
replace grade=grade[_n-1] if grade[_n-1]==grade[_n+1] & int(responseid/1000)==int(responseid[_n-1]/1000) & int(responseid/1000)==int(responseid[_n+1]/1000) & schoolname==schoolname[_n-1] & schoolname==schoolname[_n+1] & part==part[_n-1] & part==part[_n+1] & grade==. 

replace grade=grade[_n-1] if grade[_n-1]==grade[_n+2] & int(responseid/1000)==int(responseid[_n-1]/1000) & int(responseid/1000)==int(responseid[_n+2]/1000) & schoolname==schoolname[_n-1] & schoolname==schoolname[_n+2] & part==part[_n-1] & part==part[_n+2] & grade==. 

replace grade=grade[_n-2] if grade[_n-2]==grade[_n+1] & int(responseid/1000)==int(responseid[_n-2]/1000) & int(responseid/1000)==int(responseid[_n+1]/1000) & schoolname==schoolname[_n-2] & schoolname==schoolname[_n+1] & part==part[_n-2] & part==part[_n+1] & grade==. 

}

* save as new dataset 
save "$temp/grade910_baseline_wgrade.dta", replace 


*******************************************************************************
***** merge with grade information from minjiwoong school 
use "$temp/grade910_baseline.dta", clear
keep if schoolname=="minjiwoong central school"
duplicates drop name, force
tempfile minjiwoong
save `minjiwoong', replace

use "$temp/list_minjiwoong central school.dta", clear
keep if inlist(grade, 9, 10)
drop stream2
duplicates drop name2, force 
rename name2 name 
merge 1:1 name using `minjiwoong'

preserve 
keep if _merge==3 
drop _merge schoolname2 
keep studentid grade responseid
g grade_list = grade 
save "$temp/grade910_baseline_minjiwoong_p1", replace 
restore

keep if _merge~=3
sort name 
g grade_list = grade 
replace grade=10 if inlist(responseid, 9567, 9588, 9564, 19315, 19011, 19580, 19124, 19027, 19495, 19405)
replace grade=10 if responseid>=19000 & responseid<=22000 & grade==.
replace grade=9 if inlist(responseid, 15329, 16818, 16922, 16934, 16159, 16895, 16816, 16975)
replace grade=9 if responseid>=13000 & responseid<=18000 & grade==.
keep if responseid~=.
keep studentid grade responseid grade_list
save "$temp/grade910_baseline_minjiwoong_p2", replace 

* determine grade based on responseid for the remaining observations 
use "$temp/grade910_baseline.dta", clear
keep if schoolname=="minjiwoong central school"
merge 1:1 responseid using "$temp/grade910_baseline_minjiwoong_p1"
rename _merge m1
merge 1:1 responseid using "$temp/grade910_baseline_minjiwoong_p2"
rename _merge m2

br responseid name grade 
replace grade=10 if responseid>=19000 & responseid<=22000 
replace grade=10 if responseid>=9000 & responseid<13000 & grade==.
replace grade=9 if responseid>=13000 & responseid<=18000 & grade==.

save "$temp/grade910_baseline_minjiwoong.dta", replace 


*******************************************************************************
***** merge with grade information from damphu school 
use "$temp/grade910_baseline.dta", clear
keep if schoolname=="damphu central school"
duplicates drop name, force
tempfile damphu
save `damphu', replace

use "$temp/list_damphu central school.dta", clear
keep if inlist(grade, 9, 10)
drop stream2
duplicates drop name2, force 
rename name2 name 
merge 1:1 name using `damphu'

preserve 
keep if _merge==3 
drop _merge schoolname2 
keep studentid grade responseid
g grade_list = grade 
save "$temp/grade910_baseline_damphu_p1", replace 
restore

keep if _merge~=3
sort name
* compare name to retrieve grade 
g grade_list = grade 
replace grade=9 if inlist(responseid, 28590, 28364, 28233, 28112, 28156, 28122, 28557, 28159, 28369) & grade==.
replace grade=10 if inlist(responseid, 28700, 16968, 9474, 19735, 9435, 9323, 19740, 19729, 16971, 9440 ) & grade==.
keep if responseid~=.

keep studentid grade responseid grade_list
save "$temp/grade910_baseline_damphu_p2", replace 

* determine grade based on responseid for the remaining observations 
use "$temp/grade910_baseline.dta", clear
keep if schoolname=="damphu central school"
merge 1:1 responseid using "$temp/grade910_baseline_damphu_p1"
rename _merge m1
merge 1:1 responseid using "$temp/grade910_baseline_damphu_p2"
rename _merge m2

br responseid name grade 
replace grade=10 if responseid>=9000 & responseid<=28000 
replace grade=9 if responseid>28000 & responseid<=29285 
save "$temp/grade910_baseline_damphu.dta", replace 


*******************************************************************************
* put data together 
use "$temp/grade910_baseline_wgrade.dta", clear 
replace grade=10 if missing(grade) & schoolname=="lhuentse higher secondary school"
replace grade=10 if missing(grade) & responseid<15000 & schoolname=="wangchu middle secondary school"
replace grade=9 if missing(grade) & responseid>=15000 & schoolname=="wangchu middle secondary school"
drop if schoolname=="damphu central school" | schoolname=="minjiwoong central school"
append using "$temp/grade910_baseline_damphu.dta"
append using "$temp/grade910_baseline_minjiwoong.dta" 
drop m0 m1 m2

save "$clean/grade910_baseline_wgrade.dta", replace

* FOR reachable schools
keep if dn_stem~=.

save "$clean/grade910_baseline_reachable_wgrade.dta", replace

global tempfilelist: dir "$temp/" files "grade910_*.dta"
	foreach tfile of global tempfilelist {
	erase "$temp/`tfile'"
}


